package com.zlj.springcloud.controller;

import com.zlj.springcloud.entity.Dept;
import com.zlj.springcloud.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: zlj
 * @CreateDate: 2018/6/13 15:25
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeptController.class);

    private final DeptService deptService;

    private DiscoveryClient client;

    public DeptController(DeptService deptService, DiscoveryClient client) {
        this.deptService = deptService;
        this.client = client;
    }

    @GetMapping("/get/{deptNo}")
    public Dept get(@PathVariable("deptNo") Long deptNo) {
        Dept dept = deptService.get(deptNo);
        return dept;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Dept> list() {
        return deptService.list();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public boolean add(@RequestBody Dept dept) {
        return deptService.add(dept);
    }

    @RequestMapping(value = "/discovery", method = RequestMethod.GET)
    public Object getClient() {
        List<String> services = client.getServices();
        LOGGER.info("所有微服务的数量：{}", services.size());

        List<ServiceInstance> instances = client.getInstances("MICROSERVICECLOUD-DEPT");
        for (ServiceInstance instance : instances) {
            LOGGER.info("微服务的相关信息：host: {}, port: {}, serviceId: {}, uri: {}", instance.getHost(),
                    instance.getPort(), instance.getServiceId(), instance.getUri());
        }
        return this.client;
    }

}


